home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 050 / arglist.arc / ARGLIST.PAS next >
Encoding:
Pascal/Delphi Source File  |  1986-11-25  |  3.5 KB  |  121 lines

  1. { This provides capabilities similar to argc/argv in C. You can now
  2.   read the argument list from your TURBO Pascal program. `argc' is
  3.   actually a function that returns the number of parameters on the
  4.   command line. Spaces are the separators between parameters. If the
  5.   parameter is enclosed in quotes ('), then any characters can appear.
  6.   If you want a quote, put two in the command line parameter.
  7.  
  8. Example
  9.  
  10.     cmd 'this is the first' and 'this is the ''third'' argument'
  11.  
  12. This will return argc=3.
  13.  
  14. You can retrieve the arguments by calling argv(#)
  15.  
  16. Example
  17.    if the above was the command line, then the program
  18.  
  19.       for i := 1 to argc do
  20.          writeln('argv(',i,') =<',argv(i),'>');
  21.  
  22. would print
  23.  
  24.     argv(1) =<this is the first>
  25.     argv(2) =<and>
  26.     argv(3) =<this is the 'third' argument>
  27.  
  28.             Jim Holtman
  29.             35 Dogwood Trail
  30.             Randolph, NJ 07869
  31.             (201) 361-3395
  32. }
  33.  
  34. type
  35.     arglist_string = string[80];
  36. const
  37.     arglist_max = 20;
  38.     arglist_number : integer = -1;
  39. var
  40.     argvlist : array[1..arglist_max] of ^arglist_string;
  41. function argv(num : integer) : arglist_string;
  42.  
  43. var
  44.     argument : arglist_string absolute cseg:$80;
  45.     newparm,parmline : arglist_string;
  46.     i,j : integer;
  47.     state : (leading_ws, non_quote, quoted, end_quote);
  48.     inchar : char;
  49.  
  50.     procedure saveparm;
  51.     begin
  52.       if arglist_number < arglist_max then begin
  53.         arglist_number := arglist_number+1;
  54.         new(argvlist[arglist_number]);
  55.         argvlist[arglist_number]^ := newparm;
  56.         newparm := '';
  57.         end;
  58.       end;
  59.  
  60. begin
  61.     if arglist_number = -1 then begin
  62.         arglist_number := 0;
  63.         parmline := argument+' ';
  64.         state := leading_ws;
  65.         newparm := '';
  66.         for i := 1 to length(parmline) do begin
  67.             inchar := parmline[i];
  68.             case state of
  69.  
  70.                 leading_ws: begin
  71.                     if inchar = '''' then state := quoted
  72.                     else if inchar <> ' ' then begin
  73.                         newparm := newparm+inchar;
  74.                         state := non_quote;
  75.                         end;
  76.                     end;
  77.  
  78.                 non_quote: begin
  79.                     if inchar = ' ' then begin
  80.                         saveparm;
  81.                         state := leading_ws;
  82.                         end
  83.                     else newparm := newparm+inchar;
  84.                     end;
  85.  
  86.                 quoted: begin
  87.                     if inchar = '''' then state := end_quote
  88.                     else newparm := newparm+inchar;
  89.                     end;
  90.  
  91.                 end_quote: begin
  92.                     if inchar = '''' then begin
  93.                         newparm := newparm+inchar;
  94.                         state := quoted;
  95.                         end
  96.                     else if inchar <> ' ' then begin
  97.                         newparm := newparm+inchar;
  98.                         state := non_quote;
  99.                         end
  100.                     else begin
  101.                         saveparm;
  102.                         state := leading_ws;
  103.                         end;
  104.                 end;
  105.             end;
  106.         end;
  107.         end;
  108.     if (num > 0) and (num <= arglist_number) then
  109.         argv := argvlist[num]^
  110.     else argv := '';
  111.     end;
  112.  
  113. function argc : integer;
  114. var
  115.     dummy : arglist_string;
  116.  
  117. begin
  118.     if arglist_number = -1 then dummy := argv(1); {force evaluation}
  119.     argc := arglist_number;
  120.     end;
  121.